<?php
/*
Plugin Name: Live Spaces Comments Sync
Plugin URI: http://code.google.com/p/jbobos/
Description: Sync comments from your live spaces.
Author: Thomas Tsui
Version: 0.0.1
Author URI: http://blog.jbobos.org/
*/
?>
<?php
/*  Copyright 2009  Thomas Tsui (email : thomas+livesynccomments@jbobos.org)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
?>
<?php

$WP_MSNSYNC_URL;
$WP_MSNSYNC_IDS;
$WP_MSNSYNC_COMMENT_IDS;

//////////////////////////////////////////////////////////////
//Main code section
//////////////////////////////////////////////////////////////
function wp_msnsync_comment_init(){
  add_option("wp_msnsync_comment_id", '');
}// wp_msnsync_comment_init

function wp_msnsync_comment_sync_comment($postid){
  // 1.Get Live Spaces Blog Entry's Comment RSS
  //   This Entry has to be posted by Live Space Sync
  // 2.Parse the RSS Feed, and generate comment data
  // 3.Post comments
  // 4.Save comment id
  global $wpdb;
  global $WP_MSNSYNC_URL;
  global $WP_MSNSYNC_IDS;
  global $WP_MSNSYNC_COMMENT_IDS;

  $WP_MSNSYNC_URL = get_option("wp_msnsync_url");
  $WP_MSNSYNC_IDS = get_option("wp_msnsync_id");
  $WP_MSNSYNC_COMMENT_IDS = get_option("wp_msnsync_comment_id");

  //Had been synced by Live Space Sync ?
  if(($WP_MSNSYNC_IDS[$postid])) {

    // 1.Get Live Spaces Blog Entry's Comment RSS
    require_once(ABSPATH.WPINC.'/rss.php');
    $url = "http://".$WP_MSNSYNC_URL.".spaces.live.com/blog/cns!".$WP_MSNSYNC_IDS[$postid]."/comments/feed.rss";
    $rss = fetch_rss($url);
    $items = array_slice($rss->items, 0);
    if (!empty($items)) {
      foreach ( $items as $item ) {

        // 2.Parse the RSS Feed, and generate comment data
        $comment_guid    = "cns!".$item['guid'];
        if (function_exists('date_default_timezone_set')) { date_default_timezone_set('UTC'); }
        $comment_date    = strtotime($item['pubdate']);  //pubdate is a GMT time.
        $comment_author  = $item['title'];
        $comment_content = $item['description'];

        if ($WP_MSNSYNC_COMMENT_IDS) { $wp_msnsync_comment_ids_reverse = array_flip($WP_MSNSYNC_COMMENT_IDS); }
        if (!($wp_msnsync_comment_ids_reverse) or !($wp_msnsync_comment_ids_reverse[$comment_guid])) {

          // 3.Post comments
          $comment_data = array(
            'comment_post_ID' => $postid,
            'comment_author' => $wpdb->escape($comment_author),
            'comment_author_email' => '',
            'comment_author_url' => '',
            'comment_date' => date('Y-m-d H:i:s', $comment_date+get_option('gmt_offset')*3600),
            'comment_date_gmt' => date('Y-m-d H:i:s',$comment_date),
            'comment_content' => $wpdb->escape($comment_content)
          );
          $comment_id = wp_insert_comment($comment_data);

          // 4.Save comment id
          $WP_MSNSYNC_COMMENT_IDS[$comment_id]=$comment_guid;
          update_option("wp_msnsync_comment_id", $WP_MSNSYNC_COMMENT_IDS);

        }
      }
    }
  }
}// EOF wp_msnsync_comment_sync_comment

function wp_msnsync_comment_sync_all_comments(){
  global $WP_MSNSYNC_IDS;
  $WP_MSNSYNC_IDS = get_option("wp_msnsync_id");

  if (($WP_MSNSYNC_IDS) and (count($WP_MSNSYNC_IDS)>0)) {
    $post_ids = array_keys($WP_MSNSYNC_IDS);
    foreach ( $post_ids as $postid ) { wp_msnsync_comment_sync_comment($postid); }
  }
}// EOF wp_msnsync_sync_all_comments

//if first run, initialize the options
wp_msnsync_comment_init();

function wp_msnsync_comment_display(){

  $wp_msnsync_comment_ids_num_old = count(get_option("wp_msnsync_comment_id"));

  /*First, check action*/
  if(isset($_POST['sync_all_comments'])){
    wp_msnsync_comment_sync_all_comments();
  }

  $wp_msnsync_ids_num = count(get_option("wp_msnsync_id"));
  $wp_msnsync_comment_ids_num = count(get_option("wp_msnsync_comment_id"));

?>
<div class="wrap">
  <h2>Live Spaces Comments Sync</h2>
  <div class="wrap">
  You have to install and activate plugin <a href="http://privism.org/blog/live-sync/">Live Space Sync</a> first.<br>
  We just get comments of post synced by Live Space Sync, from your live space.
  </div>
  <br />
  <div class="wrap">
  Your Space: <?php echo "http://".get_option("wp_msnsync_url").".spaces.live.com/blog/";?>
  </div>
  <br />
  <div class="wrap">
  You had synced <?php echo $wp_msnsync_ids_num; ?> posts to Live Spaces.<br>
  You had synced <?php echo $wp_msnsync_comment_ids_num; ?> comments(all, 
  including <?php echo $wp_msnsync_comment_ids_num-$wp_msnsync_comment_ids_num_old; ?> new) from Live Spaces.<br>
  </div>
  <br />
  <form name="sync_comments_options" method="post" action="<?php echo $_SERVER["REQUEST_URI"]?>">
  <p class="submit"><input name="sync_all_comments" value="Sync All Comments From Live Spaces" type="submit"/></p>
  </form>
</div>
<?php
} // EOF wp_msnsync_comment_display

function wp_msnsync_comment_add_page($s){
  add_options_page('Live Spaces Comments Sync', 'Live Spaces Comments Sync',8,__FILE__,'wp_msnsync_comment_display');
  return $s;
}

add_action('admin_menu','wp_msnsync_comment_add_page');

?>